package com.sentiwt.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sentiwt.jpa.Word;
 
@Component
public class WordDAO {
    // Injected database connection:
    @PersistenceContext private EntityManager em;
 
    // Stores a new word:
    @Transactional
    public void save(Word word) {
        em.persist(word);
    }
 
    // Retrieves all the words:
    public List<Word> getAllWords() {
        TypedQuery<Word> query = em.createQuery(
            "SELECT g FROM Word g ORDER BY g.id", Word.class);
        return query.getResultList();
    }
    
    public String getSentiment(String wordName) {
    	Query q = em.createQuery("SELECT r FROM "+Word.class.getName()+" r WHERE r.name = :name");
    	q.setParameter("name", wordName);
    	Word word = (Word) q.getSingleResult();
    	return word.getSentiment();
    }
}
